﻿using System.Data;
using System.Windows;
using CAION2.Models;

namespace CAION2.ViewModels
{
    class StigmaToolTipViewModel : ViewModelBase
    {

        private DataRow _currentStigmaRow;
        private DataRow _stigma2Row;
        private DataRow _stigma3Row;
        private string _translate;
        private bool _isStigmaPlace;
        private Visibility _chainVisible;

        public bool IsStigmaPlace 
        { 
            get { return _isStigmaPlace; }
            private set
            {
                _isStigmaPlace = value;
                OnPropertyChanged("IsStigmaPlace");
            } 
        }

        public DataRow CurrentStigmaRow
        {
            get { return _currentStigmaRow; }
            set
            {
                if (Equals(value, _currentStigmaRow)) return;

                _currentStigmaRow = value;
                OnPropertyChanged("CurrentStigmaRow");

                if (CurrentStigmaRow != null)
                {
                    ChainVisible = string.IsNullOrEmpty(CurrentStigmaRow["charge_set_name"].ToString()) ? Visibility.Collapsed : Visibility.Visible;
                }
            }
        }

        public DataRow Stigma2Row
        {
            get { return _stigma2Row; }
            set
            {
                if (Equals(value, _stigma2Row)) return;

                _stigma2Row = value;
                OnPropertyChanged("Stigma2Row");
            }
        }

        public DataRow Stigma3Row
        {
            get { return _stigma3Row; }
            set
            {
                if (Equals(value, _stigma3Row)) return;

                _stigma3Row = value;
                OnPropertyChanged("Stigma3Row");
            }
        }

        public string Translate
        {
            get { return _translate; }
            set
            {
                IsStigmaPlace = !string.IsNullOrEmpty(value);

                if (Equals(value, _translate)) return;

                _translate = value;
                OnPropertyChanged("Translate");
            } 
        }

        public Visibility ChainVisible
        {
            get { return _chainVisible; }
            private set
            {
                _chainVisible = value;
                OnPropertyChanged("ChainVisible");

                if (ChainVisible == Visibility.Visible) LoadChain();
            }
        }

        public StigmaToolTipViewModel(string translate, DataRow stigmaRow)
        {
            Translate = translate;
            CurrentStigmaRow = stigmaRow;
        }

        public StigmaToolTipViewModel()
        {
            ChainVisible = Visibility.Collapsed;
        }

        private void LoadChain()
        {
            string condition = string.Format("name = '{0}'", CurrentStigmaRow["charge_set_name"]);

            DataRow chainRow = DataModel.GetRow("StigmasCharge", condition, "*", 1);

            string fields = "name, translate" + DataModel.EngFix + " AS translate, desc_long" + DataModel.EngFix + " AS desc_long, "
                            + "skillicon_name, level, quality, require_shard, skill_group_name, "
                            + "warrior, scout, mage, cleric, engineer, artist, fighter, knight, assassin, ranger, wizard, elementalist, chanter, priest, gunner, bard, rider, "
                            + "abyss_point, charge_set_name, cost_parameter, cost_end, cost_end_lv, required_sword, required_mace, "
                            + "required_dagger, required_2hsword, required_polearm, required_staff, required_bow, required_orb, required_book, "
                            + "required_gun, required_cannon, required_harp, casting_delay, delay_time, first_target, require_skill1, "
                            + "require_skill2, instant_skill, race_permitted, cost_time";

            condition = string.Format("name = '{0}'", chainRow["skill_name2"]);
            Stigma2Row = DataModel.GetRow("Stigmas", condition, fields, 1);

            condition = string.Format("name = '{0}'", chainRow["skill_name3"]);
            Stigma3Row = DataModel.GetRow("Stigmas", condition, fields, 1);

        }
    }
}
